﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using ecdl.demo.model.expression;
using System.Xml.Serialization;
using ecdl.demo.persistence;

using Ninject;

namespace ecdl.demo.model.expression {


    public class FileExistsCondition: Expression{

        private IVolumeProvider volumeProvider;

        [Inject]
        public FileExistsCondition(IVolumeProvider volumeProvider) {
            this.volumeProvider = volumeProvider;
        }

        public FileExistsCondition(string file) {
            FileName = file; 
        }


        private string fileName;

        public string FileName {
            get { return fileName; }
            set {
                string path = volumeProvider.GetFullPath(value);
                fileName = path; 
            }
        }
        

        public override ExpressionState Evaluate(ExpressionContext context) {
            bool exists  = System.IO.File.Exists(FileName);
            LeafExpressionState state = new LeafExpressionState(this);
            state.IsOk = exists;
            SetCause(state);
            return state;
        }

        private void SetCause(LeafExpressionState state) {
            string positive = String.Format("Die Datei {0} muss existieren.", FileName);
            string negative = String.Format("Die Datei {0} darf nicht existieren.", FileName);
            state.SetCause(positive, negative);
        }
    }

    // DataTransferObject
    public class FileExsistsConditionDTO : ExpressionDTO{

        public FileExsistsConditionDTO() {

        }

        public FileExsistsConditionDTO(string filename) {
            FileName = filename;
        }

        [XmlAttribute("fileName")]
        public string FileName { get; set; }

        public override Expression ToModel(ModelFactory factory) {
            FileExistsCondition condition = factory.CreateModel<FileExistsCondition>();
            condition.FileName = FileName;
            return condition;
            
        }
    }

    public class FolderExistsCondition : Expression {
        
        private IVolumeProvider volumeProvider;

        [Inject]
        public FolderExistsCondition(IVolumeProvider volumeProvider) {
            this.volumeProvider = volumeProvider;
        }

        public FolderExistsCondition(string path) {
            Path = path;
        }


        private string path;

        public string Path {
            get { return path; }
            set {
                path = volumeProvider.GetFullPath(value);
            }
        }
        
        public override ExpressionState Evaluate(ExpressionContext context) {
            bool exists = Directory.Exists(Path);
            LeafExpressionState state = new LeafExpressionState(this);
            state.IsOk = exists;
            SetCause(state);
            return state;
        }

        private void SetCause(LeafExpressionState state) {
            string positive = String.Format("Der Ordner {0} muss existieren.", Path);
            string negative = String.Format("Der Ordner {0} darf nicht existieren.", Path);
            state.SetCause(positive, negative);
        }
    }

    public class FolderExistsConditionDTO : ExpressionDTO {
        public FolderExistsConditionDTO() {
        }

        [XmlAttribute("path")]
        public string Path { get; set; }

        public override Expression ToModel(ModelFactory factory) {
            FolderExistsCondition condition = factory.CreateModel<FolderExistsCondition>();
            condition.Path = Path;
            return condition;
        }
    }
}
